package com.itboye.ihomebank;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMCore;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.login.LoginActivity;
import com.itboye.ihomebank.message.ActivitySystemMessage;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 消息
 * 
 */
public class MessageFragment extends BaseFragment implements OnClickListener {
	TextView add_shap_title_tv;
	ImageView close_icon, img_other;

	RelativeLayout contacts_system, contacts_qianyue, contacts_yuyue,
			contacts_mensuo;
	private String uid;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.contacts_layout, null);

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public int initView() {
		return 0;
	}

	@Override
	public void onMyActivityCreated() {
		add_shap_title_tv.setText("消息");
		close_icon.setVisibility(View.GONE);
		img_other.setBackgroundResource(R.drawable.messgage_xiaoxi);
		img_other.setVisibility(View.VISIBLE);
		uid=SPUtils.get(getActivity(), null, SPContants.USER_ID, "")+"";
		// contacts_system.setOnClickListener(this);
		// contacts_qianyue.setOnClickListener(this);
		// contacts_yuyue.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.contacts_system:// 系统消息
			intent = new Intent(getActivity(), ActivitySystemMessage.class);
			startActivity(intent);
			break;
		case R.id.contacts_qianyue:// 签约消息
			intent = new Intent(getActivity(), ActivitySystemMessage.class);
			startActivity(intent);
			break;
		case R.id.contacts_yuyue:// 预约消息
			intent = new Intent(getActivity(), ActivitySystemMessage.class);
			startActivity(intent);
			break;
		case R.id.contacts_mensuo:// 门锁消息
			intent = new Intent(getActivity(), ActivitySystemMessage.class);
			startActivity(intent);
			break;
		case R.id.img_other:
			if (uid.equals("")) {
				intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
			} else {
				// 此实现不一定要放在Application onCreate中
				final String userid = "tbimuser_8f85517967795eeef66c225f7883bdcb";
				// 此对象获取到后，保存为全局对象，供APP使用
				// 此对象跟用户相关，如果切换了用户，需要重新获取
				final YWIMKit mIMKit = YWAPI.getIMKitInstance(userid,
						SPContants.YW_APPKEY);
				// 开始登录
				String password = "itboye";
				IYWLoginService loginService = mIMKit.getLoginService();
				YWLoginParam loginParam = YWLoginParam.createLoginParam(userid,
						password);
				loginService.login(loginParam, new IWxCallback() {

					@Override
					public void onSuccess(Object... arg0) {
						ByAlert.alert(arg0);

						Intent intent = mIMKit.getConversationActivityIntent();//获取会话列表
				    	startActivity(intent);
					}

					@Override
					public void onProgress(int arg0) {
						// TODO Auto-generated method stub
						System.out.println(arg0 + ">>yw");
					}

					@Override
					public void onError(int errCode, String description) {
						ByAlert.alert(errCode + ":" + description);// 如果登录失败，errCode为错误码,description是错误的具体描述信息
					}
				});
			}
			break;

		default:
			break;
		}

	}
}
